<?php 
	namespace Home\Model;
	use Think\Model;

	class ProvinceModel extends Model
	{
		protected $trueTableName = "t_province";

		//获取到在用省份
		public function get_use_province($ids,$page,$rows,$sort,$order,$program_name,$province_name,$status=false)
		{
			// 当前省份的id
			// dump($ids);exit;
			$status && $map["status"] = 1;
			if(!empty($ids)){
				$map["id"] = array("in",$ids);
			}else{
				return;
			}
	        $res = $this->where($map)->select();
	        // dump($res);exit();
	        // 在用省份处理
	        $ids = [];
	        foreach ($res as $key => $value) {
	        	$ids[] = $value['id'];
	        	// $code_type = $value['type'];
	        }
	        // dump($ids);exit;
	        // dump($code_type);exit();       //省份对应码率
	      	return D("ProgramProvince")->get_program($ids,$page,$rows,$sort,$order,$program_name,$province_name);
		}

		public function get_provincename($ids,$id,$province_name)
		{
			// dump($ids);dump($id);dump($province_name);exit();
			$ids && $map['id'] = array("in",$ids);
			// $province_name?$map['province'] = array("like","%".$province_name."%"):'';
			$res = $this->where($map)->select();
			if(!$res[1]){
				foreach ($res as $key => $value) {
					$province_name1 = $value['province'];
				}
			}else{
				foreach ($res as $key => $value) {
					$province_name1 .= $value['province'].",";
				}
			}
			$a = [];
			$a['program_id'] = $id;
			$a['province_name'] = $province_name1;
			// dump($a);   
			$b = explode(",", $a['province_name']);
			array_pop($b);
			// dump($province_name);dump($b);exit;
			if($province_name!=""){
				if(in_array($province_name,$b)){
					$c['program_id'] = $id;
					$c['province_name'] = $province_name1;
				}else{
					return;
				}
				return $c;
			}else{
				return $a;
			}
		}

		public function get_all_province($page,$rows)
		{
			$res = $this->page($page,$rows)->select();
			$res1 = $this->select();
			$s['rows'] = $res;
			$total = count($res1);
			$s['total'] = $total;
			return $s;
		}

		public function delete_province($province_ids)
		{
			$map['id'] = array("in",$province_ids);
			$res = $this->where($map)->delete();
			return $res?true:false;
		}

		public function add_province($province,$status,$malv)
		{
			$data['province'] = $province;
			$data['status'] = $status;
			$data['type'] = $malv;
			$last_province = $this->add($data); 
			// dump($last_province);exit(); //添加省份的id
			// 查出所有超管的user_id
			// $user_id = D("SysUser")->where(array("role_id"=>1))->field("id")->select();
			// dump($user_id);exit();
			// foreach ($user_id as $key => $value) {
			// 	$data['province_id'] = $last_province;
			// 	$data['user_id'] = $value['id'];
			// 	$res[] = D("UserProvince")->add($data);
			// }
			return $last_province?true:false;
		}

		public function edit_select($id)
		{
			$res = $this->where(array("id"=>$id))->select();
			return $res?$res:'';
		}

		public function edit_pro($id,$status,$province,$malv)
		{
			$data['province'] = $province;
			$data['status'] = $status;
			$data['type'] = $malv;
			// dump($data);exit();
			$res = $this->where(array("id"=>$id))->save($data);
			return $res?true:false;
		}
	}
 ?>